package upload;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;

import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class MulUploadAction extends ActionSupport{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private List<File> file;
	
	private String fileContentType;
	
	private String fileFileName;
	
	private List<String> fileDesc;

	public List<File> getFile() {
		return file;
	}

	public void setFile(List<File> file) {
		this.file = file;
	}

	public String getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	public List<String> getFileDesc() {
		return fileDesc;
	}

	public void setFileDesc(List<String> fileDesc) {
		this.fileDesc = fileDesc;
	}
	
	@Override
	public String execute() throws Exception {
		/*System.out.println(file);
		System.out.println(fileContentType);
		System.out.println(fileFileName);
		System.out.println(fileDesc);*/
		ServletContext servletContext = ServletActionContext.getServletContext();
		String dir = servletContext.getRealPath("/files/");
		//System.out.println(dir);
		String[] split = fileFileName.split(",");
		System.out.println(split[0]);
		
		for (int i = 0; i < file.size(); i++) {
			
			FileOutputStream out = new FileOutputStream(dir+"/"+split[i]);
			String path = file.get(i).getAbsolutePath();
			System.out.println(path);
			FileInputStream in = new FileInputStream(path);
			
			byte [] buffer = new byte[1024];
			int len = 0;
			
			while((len = in.read(buffer)) != -1){
				out.write(buffer, 0, len);
			}
			
			out.close();
			in.close();
		}
		
		return "input";
	}

}
